package com.ruitu.health_2.ui.mall;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidkun.xtablayout.XTabLayout;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.ScreenUtils;
import com.ruitu.arad.util.SizeUtils;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.health_2.R;
import com.ruitu.health_2.mvp.model.MallModel;
import com.ruitu.health_2.mvp.presenter.MallPresenter;
import com.ruitu.health_2.ui.activity.CouponListActivity;
import com.ruitu.health_2.ui.activity.MsgActivity;
import com.ruitu.health_2.ui.activity.ProductDetailActivity;
import com.ruitu.health_2.ui.activity.SearchActivity;
import com.ruitu.health_2.ui.adapter.HorizonProListAdapter;
import com.ruitu.health_2.ui.common.util.ViewUtil;
import com.ruitu.router_module.Logg;
import com.ruitu.router_module.bean.PageModel2;
import com.ruitu.router_module.bean.home.Advert;
import com.ruitu.router_module.bean.home.AdvertContent;
import com.ruitu.router_module.bean.home.Column;
import com.ruitu.router_module.bean.home.MallHomeData;
import com.ruitu.router_module.bean.product.ProHome;
import com.ruitu.router_module.support.util.GlideImageLoader;
import com.ruitu.router_module.support.view.BaseRecyclerAdapter;
import com.ruitu.router_module.support.view.EndlessRecyclerOnScrollListener;
import com.ruitu.router_module.support.view.OnTabSelectedListenerImpl;
import com.ruitu.router_module.util.GlideUtil;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import me.dkzwm.widget.srl.SmoothRefreshLayout;
import me.dkzwm.widget.srl.config.Constants;
import me.dkzwm.widget.srl.indicator.IIndicator;

// 商城模块首页第二版 2020.01.04
public class MallHome2Fragment extends BaseFragment<MallPresenter.MallHomeP, MallModel.MallHomeM> {
    private SmoothRefreshLayout refresh;// 外层刷新控件
    private RecyclerView rcv_mall_pro;// 商品列表 带header
    private ImageView iv_back;// 返回

    private FrameLayout home_title_bar_layout;// 主页顶部title
    private LinearLayout scan_layout, ll_search;// 标题栏左上角的,搜索
    private RelativeLayout msg_layout;// 右上角消息

    private Banner banner;
    private ImageView iv_banner_holder;// banner的占位图
    private RecyclerView rcv_10_menu;// 10个菜单项,八大模块

    private ImageView iv_new_1, iv_new_2, iv_new_3;// 新人专享左,新人专享右,新人专享3

    private XTabLayout tab, tab_2;// tab tab_2是默认隐藏的tab
    private RelativeLayout rl_tab;// 包含tab_2和下边的分割线的布局
    private RecyclerView rcv_group_buy;// 团购列表
    private TextView tv_more_group;// 更多团购按钮
    /************************** 上面是view 下面是数据 **************************/
    private MallHomeData mallHomeData;// 请求的商城首页数据

    private List<Advert> advertiseList = new ArrayList<>();// banner广告轮播数据

    private HorizonProListAdapter groupBuyAdapter;// 团购商品的adapter
    private List<ProHome> groupBuyProList = new ArrayList<>();// 团购商品列表

    private MenuAdapter menuAdapter;// 10个菜单项的adapter
    private List<Column> columnList = new ArrayList<>();// 栏目的数据

    private String currPosId = "";// 记录当前选择的tab posId
    private int currPage = 1;// 当前tab下的商品页码
    private int totalPage = 1;// 当前tab下的商品总页数
    private ArrayList<ProHome> mallProList = new ArrayList<>();// tab对应的商品列表
    MallProListAdapter proAdapter;// 商品列表adapter
    private List<Column> tabColumnList = new ArrayList<>();// 栏目的数据

    public static final String[] HOME_MODELS = {"- -", "- -", "- -", "- -", "- -"};// 新五大模块的布局 精选 读书会...
    private int SCREEN_WIDTH = 0;// 屏幕的宽度
    private int tabHei = 0;// tab的高度 50dp
    private boolean isReqComplete = false;

    public static MallHome2Fragment newInstance() {
        Bundle args = new Bundle();
        MallHome2Fragment fragment = new MallHome2Fragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SCREEN_WIDTH = ScreenUtils.getScreenWidth();// 获取屏幕宽度
        tabHei = SizeUtils.dp2px(50);
        groupBuyAdapter = new HorizonProListAdapter(getActivity());
    }

    View header;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_mall_home_2, container, false);
        refresh = v.findViewById(R.id.refresh);
        rcv_mall_pro = v.findViewById(R.id.rcv_mall_pro);
        iv_back = v.findViewById(R.id.iv_back);
        home_title_bar_layout = v.findViewById(R.id.home_title_bar_layout);
        scan_layout = v.findViewById(R.id.scan_layout);
        ll_search = v.findViewById(R.id.ll_search);
        msg_layout = v.findViewById(R.id.msg_layout);
        rl_tab = v.findViewById(R.id.rl_tab);
        tab_2 = v.findViewById(R.id.tab_2);

        initRefreshView();

        header = inflater.inflate(R.layout.header_mall_home_pro_layout, null, false);
        // Banner布局
        banner = header.findViewById(R.id.banner);// Banner布局
        iv_banner_holder = header.findViewById(R.id.iv_banner_holder);// Banner布局占位图
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) banner.getLayoutParams();
        params.width = SCREEN_WIDTH;
        params.height = params.width * 900 / 1800;// banner的比例是1800 * 900
        banner.setLayoutParams(params);
        iv_banner_holder.setLayoutParams(params);
        //菜单项的布局
        rcv_10_menu = header.findViewById(R.id.rcv_10_menu);
        menuAdapter = new MenuAdapter(getActivity());
        GridLayoutManager manager5 = new GridLayoutManager(getActivity(), 5);
        rcv_10_menu.setLayoutManager(manager5);
        rcv_10_menu.setAdapter(menuAdapter);
        for (int i = 0; i < 5; i++) {
            Column model = new Column();
            model.setName("加载中");
            columnList.add(model);
        }
        menuAdapter.setDataList(columnList);
        // 团购的布局
        tv_more_group = header.findViewById(R.id.tv_more_group);
        rcv_group_buy = header.findViewById(R.id.rcv_group_buy);
        LinearLayoutManager groupBuyManager = new LinearLayoutManager(getActivity());
        groupBuyManager.setOrientation(RecyclerView.HORIZONTAL);
        rcv_group_buy.setLayoutManager(groupBuyManager);
        rcv_group_buy.setAdapter(groupBuyAdapter);
        for (int i = 0; i < 5; i++) {
            ProHome pro = new ProHome();
            pro.setName("暂无团购" + i);
            pro.setPrice((97 + i) + "");
            pro.setPic("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1581937238528&di=64fd1292a6b02a376232899bb059e277&imgtype=0&src=http%3A%2F%2Fimg.yzt-tools.com%2F20190920%2F37a9506ca0337fb18b71777bd2c68613.jpg");
            groupBuyProList.add(pro);
        }
        groupBuyAdapter.setDataList(groupBuyProList);
        // 新人专享模块 3张图片
        iv_new_1 = header.findViewById(R.id.iv_new_1);
        iv_new_2 = header.findViewById(R.id.iv_new_2);
        iv_new_3 = header.findViewById(R.id.iv_new_3);

        int[] size = ViewUtil.getHomeNewSpecial3Height(SCREEN_WIDTH);
        LinearLayout.LayoutParams paramIvNew = (LinearLayout.LayoutParams) iv_new_1.getLayoutParams();
        paramIvNew.width = size[0];
        paramIvNew.height = size[1];
        iv_new_1.setLayoutParams(paramIvNew);

        LinearLayout.LayoutParams paramIvNew2 = (LinearLayout.LayoutParams) iv_new_2.getLayoutParams();
        paramIvNew2.width = size[0];
        paramIvNew2.height = size[1];
        iv_new_2.setLayoutParams(paramIvNew2);

        LinearLayout.LayoutParams paramIvNew3 = (LinearLayout.LayoutParams) iv_new_3.getLayoutParams();
        paramIvNew3.width = size[0];
        paramIvNew3.height = size[1];
        iv_new_3.setLayoutParams(paramIvNew3);

        // tab五个模块
        tab = header.findViewById(R.id.tab);
        // 默认设置五个tab
        for (int i = 0; i < 5; i++) {
            tab.addTab(tab.newTab().setText(HOME_MODELS[i]), i);
            tab_2.addTab(tab_2.newTab().setText(HOME_MODELS[i]), i);
        }

        proAdapter = new MallProListAdapter();
        proAdapter.setHeaderView(header);
        GridLayoutManager proLayoutManager = new GridLayoutManager(getContext(), 2);
        rcv_mall_pro.setLayoutManager(proLayoutManager);
        rcv_mall_pro.setAdapter(proAdapter);

        setListeners();
        reqData();
        showProgress();

        return v;
    }

    private void initRefreshView() {
        refresh.setEnableAutoLoadMore(true);
        refresh.setDurationToCloseHeader(500);
        refresh.setDurationOfBackToKeepHeader(300);
        refresh.setEnableOverScroll(false);
//        refresh.setLoadingMinTime(1000);// 最少刷新一秒
        refresh.setMode(Constants.MODE_DEFAULT);// MODE_SCALE 仿小米下拉拉伸页面的效果
    }

    private void reqData() {
        p.reqHomeData();
    }

    // 根据tab中的posId请求商品列表
    private void reqMallProList() {
        p.reqMallProList(currPosId, 10, currPage);
    }

    @Override
    public void onReqComplete(int code, boolean isOk, Object data) {
        if (code == 1) {// 商城首页数据
            rcv_mall_pro.post(new TopHeightRunnable());// 计算顶部高度

            mallHomeData = (MallHomeData) data;
            columnList = mallHomeData.getColumnList();
            tabColumnList = mallHomeData.getTabColumnList();

            setDataToView();

            Column tabCol = tabColumnList.get(0);
            currPosId = tabCol.getPosId();
            reqMallProList();
        }
        if (code == 2) {// 商城首页的商品列表 根据tab的posId获取
            refresh.refreshComplete(true, 800);
            PageModel2<AdvertContent<ProHome>> proPage = (PageModel2<AdvertContent<ProHome>>) data;
            totalPage = proPage.getTotalPage();
            List<AdvertContent<ProHome>> adNewData = proPage.getList();
            List<ProHome> newData = new ArrayList<>();

            for (int i = 0; i < adNewData.size(); i++) {
                AdvertContent<ProHome> adPro = adNewData.get(i);
                newData.add(adPro.getInfoObject());// 封装新的商品对象
                newData.get(i).setPic(adPro.getImgUrl());// 修改商品图片url为外层的广告图片地址
            }

            if (currPage == 1) {
                mallProList.clear();
            }
            if (!isDataEmpty(newData)) {
                mallProList.addAll(newData);
                isReqComplete = true;
            } else {
                isReqComplete = false;
            }
            proAdapter.setDatas(mallProList);
        }
    }

    private void setDataToView() {
        initBanner(mallHomeData.getCarouselList());
        menuAdapter.setDataList(mallHomeData.getColumnList());// 设置栏目模块
        try {
            GlideUtil.loadImg(mallHomeData.getAdvertList().get(0).getImgUrl(), iv_new_1);
            GlideUtil.loadImg(mallHomeData.getAdvertList().get(1).getImgUrl(), iv_new_2);
            GlideUtil.loadImg(mallHomeData.getAdvertList().get(2).getImgUrl(), iv_new_3);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {// 默认设置五个tab
            for (int i = 0; i < 5; i++) {
                tab.getTabAt(i).setText(mallHomeData.getTabColumnList().get(i).getName());
                tab_2.getTabAt(i).setText(mallHomeData.getTabColumnList().get(i).getName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initBanner(final List<AdvertContent> bannerList) {
        List<String> urlList = new ArrayList<>();//图片地址集合
        List<String> introList = new ArrayList<>();//简介集合
        if (!isDataEmpty(bannerList)) {
            for (AdvertContent banner : bannerList) {
                urlList.add(banner.getImgUrl());
                introList.add("");//不显示banner简介
            }
        }
        if (isDataEmpty(urlList)) {
            banner.setVisibility(View.GONE);
            iv_banner_holder.setVisibility(View.VISIBLE);
            return;
        } else {
            banner.setVisibility(View.VISIBLE);
            iv_banner_holder.setVisibility(View.GONE);
        }
        try {
            banner.setImages(urlList);//设置图片集合
            banner.setImageLoader(new GlideImageLoader());//设置图片加载器
            banner.setDelayTime(6000);//设置轮播时间
            banner.setIndicatorGravity(BannerConfig.RIGHT);
            banner.setBannerAnimation(Transformer.Accordion);
            banner.setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(int pos) {
                    ToastUtils.showShortSafe("点击广告banner" + pos);
                }
            });
            banner.start();//banner设置方法全部调用完毕时最后调用
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View view) {
        if (view == iv_back) {
            getActivity().finish();
        }
        if (view == msg_layout) {
            startActivity(MsgActivity.class);
        }
        if (view == scan_layout) {

        }
        if (view == ll_search) {// 搜索
            startActivity(SearchActivity.class);
        }
        if (view == iv_new_1) {
            ToastUtils.showShortSafe("活动");
        }
        if (view == iv_new_2) {
            ((MallMainActivity) getActivity()).setCurrVp(2);
        }
        if (view == iv_new_3) {// 优惠券
            startActivity(CouponListActivity.class);
        }
        if (view == tv_more_group) {// 更多团购
            ((MallMainActivity) getActivity()).setCurrVp(2);
        }
    }

    private Boolean isLoadItem = false;

    private void loadMorePro() {
        currPage++;
        if (currPage <= totalPage) {
            hideProgress();
            showProgressWithText(true, "更多数据加载中..");
            reqMallProList();
        } else {
//            ToastUtils.showShort("数据加载完成");
        }
    }

    private void setListeners() {
        setOnClickListener(iv_back, scan_layout, ll_search, msg_layout, iv_new_1, iv_new_2, iv_new_3, tv_more_group);
        menuAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                String columnId = columnList.get(position).getId();
                String columnName = columnList.get(position).getName();
                if (TextUtils.isEmpty(columnId)) {
                    ToastUtils.showShortSafe("数据加载中,请稍候重试");
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putString("columnId", columnId);
                bundle.putString("columnName", columnName);
                startActivity(ProductListByColumnActivity.class, bundle);
            }
        });
//        rcv_mall_pro.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//                super.onScrollStateChanged(recyclerView, newState);
//            }
//
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//                Logg.i("onScrolled...dy = " + dy + "   dx = " + dx);
//                GridLayoutManager manager = (GridLayoutManager) recyclerView.getLayoutManager();
//                int lastItem = manager.findLastVisibleItemPosition() + 1;
//                int totalItem = manager.getItemCount();
////                Toast.makeText(MainActivity.this, "last item is" + lastItem + "/" + totalItem, Toast.LENGTH_SHORT).show();
//                if (lastItem + 1 == totalItem) {
//                    if (isLoadItem) {
////                        Toast.makeText(MainActivity.this, "已经加载", Toast.LENGTH_SHORT).show();
//                    } else {
//                        loadMorePro();
//                    }
//                    isLoadItem = !isLoadItem;
//                }
//            }
//        });
        rcv_mall_pro.addOnScrollListener(new EndlessRecyclerOnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                GridLayoutManager manager = (GridLayoutManager) recyclerView.getLayoutManager();
                int firstPosition = manager.findFirstVisibleItemPosition();
//                Logg.i("firstPosition = " + firstPosition);
//                if (firstPosition > 0) {// 这个逻辑是正确的
//                    rl_tab.setVisibility(View.VISIBLE);
//                } else {
//                    rl_tab.setVisibility(View.GONE);
//                }
                int scrollY = rcv_mall_pro.computeVerticalScrollOffset();
                // 这个逻辑也是正确的
//                if (scrollY > topHeight) {// 滑动超过了顶部(banner + menu + 3图)
//                    rl_tab.setVisibility(View.VISIBLE);
//                } else {
//                    rl_tab.setVisibility(View.GONE);
//                }
//                Logg.i("onScrolled...topHeight = " + topHeight + "   scrollY = " + scrollY);
                if ((scrollY + tabHei > topHeight || firstPosition > 0) && isReqComplete) {// 最终方案
                    rl_tab.setVisibility(View.VISIBLE);
                } else {
                    rl_tab.setVisibility(View.GONE);
                }
            }

            @Override
            public void onLoadMore() {
                loadMorePro();
            }
        });
        refresh.setOnRefreshListener(new SmoothRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefreshing() {
                currPage = 1;
//                reqData();
                reqMallProList();
                hideProgress();
                showProgress();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refresh.refreshComplete(false, 800);
                    }
                }, 15000);
            }

            @Override
            public void onLoadingMore() {
                Logg.i("refresh加载更多...");
            }
        });
        refresh.addOnStatusChangedListener(new SmoothRefreshLayout.OnStatusChangedListener() {
            @Override
            public void onStatusChanged(byte old, byte now) {
//                Logg.i("onStatusChanged... old = " + old + ",now = " + now);
                if (now == SmoothRefreshLayout.SR_STATUS_INIT) {// 正常状态,显示搜索栏
                    showTitleBar(true);
                } else if (now == SmoothRefreshLayout.SR_STATUS_PREPARE) {// 正在下拉
                    showTitleBar(false);
                } else if (now == SmoothRefreshLayout.SR_STATUS_REFRESHING) {// 正在刷新
                    showTitleBar(false);
                } else if (now == SmoothRefreshLayout.SR_STATUS_COMPLETE) {// 刷新完成(还没有恢复)
                    showTitleBar(false);
                } else if (now == SmoothRefreshLayout.SR_STATUS_LOADING_MORE) {// 看意思是加载更多
                    showTitleBar(true);
                }
            }
        });
        refresh.addOnNestedScrollChangedListener(new SmoothRefreshLayout.OnNestedScrollChangedListener() {
            @Override
            public void onNestedScrollChanged() {
//                Logg.i("onNestedScrollChanged...");
            }
        });
        refresh.addOnUIPositionChangedListener(new SmoothRefreshLayout.OnUIPositionChangedListener() {
            @Override
            public void onChanged(byte status, IIndicator indicator) {
                // 这个回调和addOnStatusChangedListener回调的状态值是一样的
                // 区别:OnStatusChanged状态相同时不执行,只有状态改变时执行 onChanged:一直回调当前状态
//                Logg.i("OnUIPositionChanged... status = " + status);
//                Logg.i("indicator.getRawOffset()  = " + indicator.getRawOffset());

//                if ((status == SmoothRefreshLayout.SR_STATUS_PREPARE)
//                        && (scrollY > SizeUtils.dp2px(5))) {// 下拉超过10dp
//                    showTitleBar(false);
//                }
            }
        });
        tab.addOnTabSelectedListener(new OnTabSelectedListenerImpl() {
            @Override
            public void onTabSelected(XTabLayout.Tab tab) {
                // 切换tab 请求新数据
                Column tabCol = tabColumnList.get(tab.getPosition());
                currPosId = tabCol.getPosId();// 当前切换的tab对应的posId
                currPage = 1;
                totalPage = 1;
//                mallProList.clear();
//                proAdapter.setDatas(mallProList);
                reqMallProList();
                hideProgress();
                showProgress();
                // tab1 和 tab2 联动
                tab_2.getTabAt(tab.getPosition()).select();
            }
        });
        tab_2.addOnTabSelectedListener(new OnTabSelectedListenerImpl() {
            @Override
            public void onTabSelected(XTabLayout.Tab tab) {
                // tab1 和 tab2 联动
                MallHome2Fragment.this.tab.getTabAt(tab.getPosition()).select();
            }
        });
        proAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, Object data) {
                Bundle bundle = new Bundle();
                bundle.putString("id", mallProList.get(position).getId());
                startActivity(ProductDetailActivity.class, bundle);
            }
        });
    }

    private void showTitleBar(boolean isShow) {
        if (isShow) {
            if (home_title_bar_layout.getVisibility() == View.GONE) {
                home_title_bar_layout.setVisibility(View.VISIBLE);
            }
        } else {
            if (home_title_bar_layout.getVisibility() == View.VISIBLE) {
                home_title_bar_layout.setVisibility(View.GONE);
            }
        }
    }

    //banner下面的菜单项的adapter
    private static class MenuAdapter extends ListBaseAdapter<Column> {
        public MenuAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.home_item_menu;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int pos) {
            TextView tv_menu_txt = holder.getView(R.id.tv_menu_txt);
            ImageView iv_menu_icon = holder.getView(R.id.iv_menu_icon);

            Column model = mDataList.get(pos);
            tv_menu_txt.setText(model.getName());
            GlideUtil.loadImg(model.getImgUrl(), iv_menu_icon);
        }
    }

    private class MallProListAdapter extends BaseRecyclerAdapter<ProHome> {

        @Override
        public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
            View v = View.inflate(getActivity(), R.layout.item_common_pro_list, null);
//            return new MyHolder(v);
            return new SuperViewHolder(v);
        }

        @Override
        public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, ProHome data) {
            SuperViewHolder holder = (SuperViewHolder) viewHolder;
            RelativeLayout rl_pro = holder.getView(R.id.rl_item);// 图片
            ImageView icon = holder.getView(R.id.icon);// 图片
            TextView tv_name = holder.getView(R.id.tv_name);// 名称
            TextView tv_price = holder.getView(R.id.tv_price);// 价格
            TextView tv_integral = holder.getView(R.id.tv_integral);// 积分

            RelativeLayout.LayoutParams paramsPro = (RelativeLayout.LayoutParams) rl_pro.getLayoutParams();
            if (RealPosition % 2 == 0) {
                paramsPro.leftMargin = SizeUtils.dp2px(10);
                paramsPro.rightMargin = SizeUtils.dp2px(5);
            } else {
                paramsPro.leftMargin = SizeUtils.dp2px(5);
                paramsPro.rightMargin = SizeUtils.dp2px(10);
            }
            int num = mallProList.size();
            if (num % 2 == 0) {
                if (RealPosition == 0 || RealPosition == 1) {
                    paramsPro.topMargin = SizeUtils.dp2px(10);
                    paramsPro.bottomMargin = SizeUtils.dp2px(5);
                } else if (RealPosition == num - 1 || RealPosition == num - 2) {
                    paramsPro.topMargin = SizeUtils.dp2px(5);
                    paramsPro.bottomMargin = SizeUtils.dp2px(10);
                } else {
                    paramsPro.topMargin = SizeUtils.dp2px(5);
                    paramsPro.bottomMargin = SizeUtils.dp2px(5);
                }
            } else {
                if (RealPosition == 0 || RealPosition == 1) {
                    paramsPro.topMargin = SizeUtils.dp2px(10);
                    paramsPro.bottomMargin = SizeUtils.dp2px(5);
                } else if (RealPosition == num - 1) {
                    paramsPro.topMargin = SizeUtils.dp2px(5);
                    paramsPro.bottomMargin = SizeUtils.dp2px(10);
                } else {
                    paramsPro.topMargin = SizeUtils.dp2px(5);
                    paramsPro.bottomMargin = SizeUtils.dp2px(5);
                }
            }

            int width = ScreenUtils.getScreenWidth();
            int itemWidth = (width - SizeUtils.dp2px(30)) / 2;

            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) icon.getLayoutParams();
            params.width = itemWidth;
            params.height = itemWidth;
            icon.setLayoutParams(params);

            ProHome pro = data;
            tv_name.setText(pro.getName());
            tv_price.setText("￥" + pro.getPrice());
            GlideUtil.loadImg(pro.getPic(), icon);

            try {
                if (Integer.parseInt(pro.getGiftPoint()) > 0) {
                    tv_integral.setText("送" + pro.getGiftPoint() + "积分");
                } else {
                    tv_integral.setText("");
                }
            } catch (Exception e) {
                e.printStackTrace();
                tv_integral.setText("");
            }
        }

//        class MyHolder extends BaseRecyclerAdapter.Holder {
//            public MyHolder(View itemView) {
//                super(itemView);
//            }
//        }
    }

    private int tabY = 0;// tab的y坐标
    private int topHeight = 0;// 记录页面上部的高度(banner + menu + 3图 + tab + 上下外边距)

    // 计算首页顶部高度
    private class TopHeightRunnable implements Runnable {
        @Override
        public void run() {
            topHeight = header.getMeasuredHeight();

            int[] position = new int[2];
            tab.getLocationInWindow(position);
            tabY = position[1];
        }
    }
}
